package com.blb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blb.entity.Shelf;
import com.blb.service.IShelfService;
import com.blb.utils.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *仓库管理中的货架管理
 * @author wss
 * @since 2020-05-20
 */
@RestController
@RequestMapping("/shelf")
public class ShelfController {
    @Autowired
    private IShelfService shelfService;

    /**
     * 分页查询货架
     * @param cTime
     * @param draw
     * @param shelfNo
     * @param id
     * @param start
     * @param length
     * @return
     */
    @RequiresPermissions("storyjia")
    @RequestMapping("shelf_manage")
    public DataTablesPage<Shelf> shelf_manage(String cTime, Integer draw, String shelfNo, String id, Integer start, Integer length) {
        Map<String, Object> map = new HashMap<>();
        String companyId = ShiroUtils.getCompany();
        map.put("companyId", companyId);
        if (StringUtils.isEmpty(cTime)){
            map.put("cTime",null);
        }else {
            map.put("cTime",'%'+cTime+"%");
        }
        map.put("shelfNo", shelfNo);
        map.put("id", id);
        int pageCount = shelfService.getPageCount(map);
        map.put("start", start);
        map.put("length", length);
        List<Shelf> shelfList = shelfService.getShelfByArea(map);
        for (Shelf shelf : shelfList) {
            shelf.setAreaName(shelf.getWarehouseName() + shelf.getAreaName());
        }
        return new DataTablesPage<>(draw, pageCount, pageCount, shelfList);
    }

    /**
     * 添加货架信息
     * @param shelf
     * @return
     */
    @RequiresPermissions("storyjia:add")
    @RequestMapping("add")
    public Response add(Shelf shelf) {
        shelf.setId(UUIDUtils.getId());
        shelf.setCompanyId(ShiroUtils.getCompany());
        shelf.setCreateTime(TimeUtils.getNowTime());
        shelfService.save(shelf);
        return Response.success("添加成功");
    }

    /**
     * 修改货架信息
     * @param shelf
     * @return
     */
    @RequiresPermissions("storyjia:update")
    @RequestMapping("update")
    public Response update(Shelf shelf) {
        shelfService.updateById(shelf);
        return Response.success("修改成功");
    }

    /**
     * 修改货架状态
     * @param shelf
     * @return
     */
    @RequiresPermissions("storyjia:update")
    @RequestMapping("updateStatus")
    public Response updateStatus(Shelf shelf) {
        Shelf pojo = shelfService.getById(shelf.getId());
        pojo.setShelfStatus(shelf.getShelfStatus());
        shelfService.updateById(pojo);
        return Response.success("修改成功");
    }

    /**
     * 根据货区id 公司id 查询货架
     * @param areaId
     * @return
     */
    @RequiresPermissions("storyjia")
    @RequestMapping("listByAid")
    public List<Shelf> listByAid(String areaId) {
        String companyId = ShiroUtils.getCompany();
        QueryWrapper<Shelf> wrapper = new QueryWrapper<>();
        wrapper.eq("area_id", areaId).eq("company_id", companyId);
        return shelfService.list(wrapper);
    }


}

